package xin.yangda.poiplus.test.service.impl;

import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;
import xin.yangda.poiplus.constants.PoiPlusFileExtend;
import xin.yangda.poiplus.designer.SimpleXlsDesigner;
import xin.yangda.poiplus.designer.csv.CsvDesigner;
import xin.yangda.poiplus.pojo.ExcelData;
import xin.yangda.poiplus.test.entity.GoodsEntity;
import xin.yangda.poiplus.test.entity.OrderEntity;
import xin.yangda.poiplus.test.service.OrderService;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author yangdaxin
 * @version 创建时间 2019/1/17 14:28
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Override
    public void exportExcelByTemplate(String tempPath, PoiPlusFileExtend poiPlusFileExtend) {
        // 打开模板
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(tempPath);
        try (SimpleXlsDesigner simpleXlsDesigner = new SimpleXlsDesigner(inputStream)) {
            // 填充数据源
            ExcelData<OrderEntity> excelData = new ExcelData<>(OrderEntity.class, this.queryList());
            simpleXlsDesigner.setData(excelData);

            // 数据加工
            simpleXlsDesigner.process();

            // 保存Excel
            String saveFilePath = "D:\\OrderData" + poiPlusFileExtend.getValue();
            simpleXlsDesigner.saveFile(saveFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportExcel(PoiPlusFileExtend poiPlusFileExtend) {
        try (SimpleXlsDesigner simpleXlsDesigner = new SimpleXlsDesigner()) {
            // 填充数据源
            ExcelData<OrderEntity> excelData = new ExcelData<>(OrderEntity.class, this.queryList());
            simpleXlsDesigner.setData(excelData);

            // 数据加工
            simpleXlsDesigner.process();

            // 保存Excel
            String saveFilePath = "D:\\OrderData" + poiPlusFileExtend.getValue();
            simpleXlsDesigner.saveFile(saveFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportTemplate(PoiPlusFileExtend poiPlusFileExtend) {
        try (SimpleXlsDesigner simpleXlsDesigner = new SimpleXlsDesigner()) {
            // 填充数据源
            ExcelData<OrderEntity> excelData = new ExcelData<>(OrderEntity.class);
            simpleXlsDesigner.setData(excelData);

            // 模板加工
            simpleXlsDesigner.process(true);

            // 保存Excel
            String saveFilePath = "D:\\OrderTemplate" + poiPlusFileExtend.getValue();
            simpleXlsDesigner.saveFile(saveFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportCsv() {
        ExcelData<OrderEntity> excelData = new ExcelData<>(OrderEntity.class, this.queryList());

        String saveFilePath = "D:\\OrderData.csv";
        CsvDesigner.build(excelData).export(saveFilePath);
    }

    @Override
    public List<OrderEntity> queryList() {
        BigDecimal money = new BigDecimal(100);
        return Lists.newArrayList(
                OrderEntity.builder()
                        .id(1)
                        .money(money)
                        .goodsCount(1)
                        .goods(GoodsEntity.builder().id(1).name("钢笔").build())
                        .createTime(new Date())
                        .build()
        );
    }
}
